import router from '@/router'
const loadView = (path) => {
  return () => import('@v/' + path)
}
const filterRoutes = (routes) => {
  console.log('22222');
  const authRoutes = []
  routes.forEach((route) => {
    route.component = loadView(route.component)
    if (route.children) {
      route.children = filterRoutes(route.children)
    }
    authRoutes.push(route)
  })

  return authRoutes
}
export default {
  namespaced: true,
  state: {
    token: localStorage.getItem('token') || '',
    role: localStorage.getItem('role') || '',
    userInfo: localStorage.getItem('userInfo')
      ? JSON.parse(localStorage.getItem('userInfo'))
      : {},
    menus: localStorage.getItem('authMenus')
      ? JSON.parse(localStorage.getItem('authMenus'))
      : []
  },
  mutations: {
    INIT_USER (state, { token, role, userInfo }) {
      state.token = token
      state.role = role
      state.userInfo = userInfo
    },
    INIT_MENUS (state, menus) {
      state.menus = menus
    },
    ADD_AUTH_ROUTES (state, routes) {
      const authRoutes = filterRoutes(routes)
      authRoutes.forEach((route) => {
        router.addRoute('layout', route)
        
      })
      router.getRoutes()
    }
  }
}
